﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetworkModel;
using DLULearningNetwork.ActionFilterAttributes;

namespace DLULearningNetwork.Controllers
{
    public class RightBoxController : Controller
    {
        [RequireUserLogin()]
        public ActionResult GetFriendRequestBox()
        {
            Account currentUser = RequireUserLogin.LoginUser;
            int totalItem = 0;
            List<Friend> lstFriendRequest = Friend.SelectFriendRequestForAnAccount(currentUser.AccountID_u, 0, 10, ref totalItem);
            return PartialView(lstFriendRequest);
        }
        [RequireUserLogin()]
        public ActionResult GetScheduleSermina()
        {
            Account currentUser = RequireUserLogin.LoginUser;
            List<Course> lstCourse = Course.GetCourseToday(currentUser.AccountID_u);
            return PartialView(lstCourse);
        }
        [RequireUserLogin()]
        public ActionResult GetFriendListBox(Guid ownerAccountID, string nickname)
        {
            Account curUser = RequireUserLogin.LoginUser;
            if (string.IsNullOrEmpty(nickname))
            {
                nickname = curUser.Nickname_s;
            }
            Account accountOwner = Account.Single(nickname);
            ViewBag.AccountOwner = accountOwner;
            Profile profileOfAccountOwner = Profile.Single(accountOwner.AccountID_u);

            bool isFriend = Friend.IsFriend(accountOwner.AccountID_u, curUser.AccountID_u);
            bool showMakeFriend = !isFriend;
            bool showFriendList = IsPermit(accountOwner, curUser, isFriend, profileOfAccountOwner.ProfileID_u, EnumPrivacyFlagType.ShowFriendList);

            if (accountOwner.AccountID_u == curUser.AccountID_u)
            {
                showFriendList = true;
            }
            //Kiem tra quyen xem danh sach ban be

            ViewBag.ShowFriendList = showFriendList;
            Account currentUser = RequireUserLogin.LoginUser;
            int totalItem = 0;
            List<Friend> lstFriend = null;
            if (showFriendList)
            {
                lstFriend = Friend.SelectFriendOfAnAccount(ownerAccountID, 1, 10, ref totalItem);

            }

            return PartialView(lstFriend);


        }

        bool IsPermit(Account accountOwner, Account curUser, bool isFriend, Guid profileIdOfCurUser, EnumPrivacyFlagType enumPrivacyFlagType)
        {
            bool isPermit = true;
            PrivacyFlag flag = PrivacyFlag.SelectByFlagType(profileIdOfCurUser, enumPrivacyFlagType);
            if (flag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Private.ToString())
            {
                if (accountOwner.AccountID_u != curUser.AccountID_u)
                {
                    isPermit = false;
                }
            }
            else
            {
                if (flag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Friends.ToString())
                {
                    if (!isFriend)
                    {
                        isPermit = false;
                    }
                }
            }
            return isPermit;
        }
        [RequireUserLogin()]
        public ActionResult NotificationBox()
        {
            return PartialView();
        }

       
    }
}
